<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/6/23 0023
 * Time: 6:18
 */

namespace Qwadmin\Controller;
use Qwadmin\Controller\ComController;

class SalonController extends ComController{
    public function add(){
        $this -> display();
    }

    public function index($sid=0,$p=1){

        $p = intval($p)>0?$p:1;

        $article = M('salon');
        $pagesize = 20;#每页数量
        $offset = $pagesize*($p-1);//计算记录偏移量
        $count = $article->count();
        $list  = $article->order("addtime desc")->limit($offset.','.$pagesize)->select();

        $page	=	new \Think\Page($count,$pagesize);
        $page = $page->show();
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this -> display();
    }

    public function del(){

        $aids = isset($_REQUEST['aids'])?$_REQUEST['aids']:false;
        if($aids){
            if(is_array($aids)){
                $aids = implode(',',$aids);
                $map['id']  = array('in',$aids);
            }else{
                $map = 'id='.$aids;
            }
            if(M('salon')->where($map)->delete()){
                addlog('删除文章，AID：'.$aids);
                $this->success('恭喜，文章删除成功！');
            }else{
                $this->error('参数错误！');
            }
        }else{
            $this->error('参数错误！');
        }

    }

    public function edit($aid){

        $aid = intval($aid);
        $article = M('salon')->where('id='.$aid)->find();
        if($article){
            $this->assign('article',$article);
        }else{
            $this->error('参数错误！');
        }
        $this -> display();
    }

    public function update($aid=0){
        $aid = intval($aid);
        $data['title'] = isset($_POST['title'])?$_POST['title']:false;
        $data['link'] = isset($_POST['link'])?$_POST['link']:'';
        $data['status'] = isset($_POST['status'])?$_POST['status']:false;
        $data['addtime'] = time();
        if($aid){
            M('salon')->data($data)->where('aid='.$aid)->save();
            addlog('编辑文章，AID：'.$aid);
            $this->success('恭喜！文章编辑成功！');
        }else{
            $aid = M('salon')->data($data)->add();
            if($aid){
                addlog('新增文章，AID：'.$aid);
                $this->success('恭喜！文章新增成功！');
            }else{
                $this->error('抱歉，未知错误！');
            }

        }
    }
}